package com.usian.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.search.ApAssociateWords;
import com.usian.model.search.UserSearchDto;
import com.usian.model.search.utils.Trie;
import com.usian.search.mapper.ApAssociateWordsMapper;
import com.usian.search.service.ApAssociateWordsService;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: ApAssociateWordsServiceImpl
 * Author:   WGG
 * Date:    2022-12-2022/12/11-8:53
 * Version: 1.0
 * Description:
 */
@Service
public class ApAssociateWordsServiceImpl extends ServiceImpl<ApAssociateWordsMapper, ApAssociateWords> implements ApAssociateWordsService {

    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 联想词模糊查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findByLike(UserSearchDto dto) {
        //校验参数
        if (dto.getPageSize()>50){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //模糊查询
        Page pageInfo = new Page<>(dto.getPageNum(),dto.getPageSize());
        LambdaQueryWrapper<ApAssociateWords>  queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(ApAssociateWords::getAssociateWords,dto.getSearchWords());
        IPage page = page(pageInfo, queryWrapper);
        ResponseResult responseResult = new PageResponseResult(dto.getPageNum(),dto.getPageSize(),(int)page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * redis优化联想词查询
     * @param dto
     * @return
     */
    @Override
    public ResponseResult searchRedis(UserSearchDto dto) {
        //从缓存中获取数据
        String associateList = redisTemplate.opsForValue().get("associate_list");
        List<ApAssociateWords> apAssociateWords = null;
        //判断数据是否存在
        if (StringUtils.isNotEmpty(associateList)){
            //保存到集合
            apAssociateWords = JSON.parseArray(associateList, ApAssociateWords.class);
        }else {
            //去查询，并存入redis
            apAssociateWords = list();
            redisTemplate.opsForValue().set("associate_list", JSON.toJSONString(apAssociateWords));
        }
        //构建Trie数据结构，从trie中获取数据，封装返回
        Trie trie = new Trie();
        //将联想词保存在trie数据结构中
        for (ApAssociateWords apAssociateWord : apAssociateWords) {
            trie.insert(apAssociateWord.getAssociateWords());
        }


        //通过trie的startWidth查询
        List<String> ret = trie.startWith(dto.getSearchWords());
        List<ApAssociateWords> resultList = new ArrayList<>();
        //将关联词条保存在关联词对象返回
        for (String AssociateWord : ret) {
            ApAssociateWords apAssociate= new ApAssociateWords();
            apAssociate.setAssociateWords(AssociateWord);
            resultList.add(apAssociate);
        }
        return ResponseResult.okResult(resultList);
    }
}
